﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Project.Core.Common
{
    public class PageDataList<T>
    {
        private int _pageIndex = 1;
        private int _pageSize = 20;
        public PageDataList()
        { }
        public PageDataList(List<T> entities, int pageIndex = 1, int pageSize = 20)
        {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            PageData = entities;
        }

        public PageDataList(int count, List<T> entities, int pageIndex = 1, int pageSize = 20)
        {
            TotalCount = count;
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            PageData = entities;
        }

        /// <summary>
        /// 分页参数信息
        /// </summary>
        //[DataMember]
        //public PageInfo PageInfo { get; private set; }
        /// <summary>
        /// 本业数据实体
        /// </summary>
        public List<T> PageData { get; private set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get
            {
                int pages = 0;
                if (TotalCount > 0 && PageSize > 0)
                {
                    int temp = TotalCount % PageSize;
                    pages = TotalCount / PageSize;
                    if (temp > 0)
                    {
                        return pages + 1;
                    }
                }

                return pages;
            }
        }

        public int PageIndex
        {
            get
            {
                if (_pageIndex < 1)
                {
                    _pageIndex = 1;
                }
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                if (_pageSize < 1)
                {
                    _pageSize = 20;
                }
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }
    }
}
